import Vue from 'vue'
import App from './App.vue'
import router from "./router"
import store from "./store"
import axios from "axios"
import Storage from './plugins/storage.js'
import "./assets/fonts/iconfont.css"
import "./assets/css/global.css"

// 添加进度条效果
import NProgress from 'nprogress'

// 富文本插件
import VueQuillEditor from 'vue-quill-editor'
Vue.use(VueQuillEditor)
// markdown插件
import mavonEditor from 'mavon-editor'
Vue.use(mavonEditor)

// 全局配置elementui的dialog不能通过点击遮罩层关闭
// ElementUI.Dialog.props.closeOnClickModal.default = false

// 简单配置
NProgress.inc(0.2)
NProgress.configure({easing: 'ease', speed: 500, showSpinner: false, trickle: false})

// 配置请求根路径
axios.defaults.baseURL = "http://127.0.0.1:8000/api/v1"
axios.interceptors.request.use(config => {
    NProgress.start()
    config.headers.Authorization = Storage.sessionGet("token");
    return config
})

axios.interceptors.response.use(response => {
    NProgress.done()
    switch (true) {
        case response.status >= 250 && response.status <= 260:
            Storage.sessionRemove('token') // 删除已经失效或过期的token（不删除也可以，因为登录后覆盖）
            router.replace({path: '/Login'}).catch(err => {err})
            break
        case response.data.token:
            Storage.sessionSet('token', response.data.token)
        default:
            return response
    }
}, error => {
    NProgress.done()
    switch (error.response.status) {
        case 403:
            ElementUI.Message({message:"权限不足", type:"error"})
            break
        default:
            ElementUI.Message({message:"服务器响应失败", type:"error"})
            break
    }
    return Promise.reject(error)
})

Vue.filter("datetimeFormat", function (time) {
    let oldDate = new Date(time)
    let year = oldDate.getFullYear();
    let month = oldDate.getMonth() + 1;
    let day = oldDate.getDate();
    let hour = oldDate.getHours();
    let minute = oldDate.getMinutes();
    let second = oldDate.getSeconds();
    return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
})

Vue.filter("dateFormat", function (time) {
    if (time == '0001-01-01T00:00:00Z') return ''
    let oldDate = new Date(time)
    let year = oldDate.getFullYear();
    let month = oldDate.getMonth() + 1;
    let day = oldDate.getDate();
    let hour = oldDate.getHours();
    let minute = oldDate.getMinutes();
    let second = oldDate.getSeconds();
    return year + "-" + month + "-" + day
})


Vue.config.productionTip = false
Vue.prototype.domain = "http://127.0.0.1:8000"
Vue.prototype.storage = Storage
Vue.prototype.axios = axios


new Vue({
    router,
    store,
    render: h => h(App),
}).$mount('#app')
